
package com.usersmodule.dao;
import com.usersmodule.bean.*;
import java.sql.*;
import java.util.*;

import util.*;
public class TbUsersDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbUsers tbUsers=null;
    private List<TbUsers> tbUsersList=null;

    public boolean add(TbUsers inTbUsers){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Users(UserName,UserNickName,UserCardId,UserWorkId,UserPass,UserPhone,UserEmail,UserAddress,UserBirthday,UserInCompanyTime,UserState,ParentUserId,CreateUserId,IsEnable,UserMemo) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbUsers.getUserName());
        pt.setString(2,inTbUsers.getUserNickName());
        pt.setString(3,inTbUsers.getUserCardId());
        pt.setString(4,inTbUsers.getUserWorkId());
        pt.setString(5,inTbUsers.getUserPass());
        pt.setString(6,inTbUsers.getUserPhone());
        pt.setString(7,inTbUsers.getUserEmail());
        pt.setString(8,inTbUsers.getUserAddress());
        pt.setString(9,inTbUsers.getUserBirthday());
        pt.setString(10,inTbUsers.getUserInCompanyTime());
        pt.setInt(11,inTbUsers.getUserState());
        pt.setInt(12,inTbUsers.getParentUserId());
        pt.setInt(13,inTbUsers.getCreateUserId());
        pt.setInt(14,inTbUsers.getIsEnable());
        pt.setString(15,inTbUsers.getUserMemo());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inUserId){
        return UtilDAO.del("Tb_Users", "UserId", inUserId);
    }
    public boolean edit(TbUsers inTbUsers){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Users set  UserName=?,UserNickName=?,UserCardId=?,UserWorkId=?,UserPass=?,UserPhone=?,UserEmail=?,UserAddress=?,UserBirthday=?,UserInCompanyTime=?,UserState=?,ParentUserId=?,CreateUserId=?,IsEnable=?,UserMemo=? where UserId=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbUsers.getUserName());
        pt.setString(2,inTbUsers.getUserNickName());
        pt.setString(3,inTbUsers.getUserCardId());
        pt.setString(4,inTbUsers.getUserWorkId());
        pt.setString(5,inTbUsers.getUserPass());
        pt.setString(6,inTbUsers.getUserPhone());
        pt.setString(7,inTbUsers.getUserEmail());
        pt.setString(8,inTbUsers.getUserAddress());
        pt.setString(9,inTbUsers.getUserBirthday());
        pt.setString(10,inTbUsers.getUserInCompanyTime());
        pt.setInt(11,inTbUsers.getUserState());
        pt.setInt(12,inTbUsers.getParentUserId());
        pt.setInt(13,inTbUsers.getCreateUserId());
        pt.setInt(14,inTbUsers.getIsEnable());
        pt.setString(15,inTbUsers.getUserMemo());
        pt.setInt(16,inTbUsers.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbUsers getTbUsersByUserId(int inUserId) {
    	tbUsers=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Users where UserId="+inUserId;
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            if (rs.next()) {
                tbUsers = getTbUsers(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbUsers;
    }
    public TbUsers checkLogin(TbUsers inTbUser) {
        try {
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Users where userName=? and userPass=?";
            pt = conn.prepareStatement(sql);
            pt.setString(1, inTbUser.getUserName());
            pt.setString(2, inTbUser.getUserPass());
            rs = pt.executeQuery();
            if (rs.next()) {
               tbUsers=this.getTbUsers(rs);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           ConnectDB.closeSqlConnection();
        }
		return tbUsers;
	}
    public List<TbUsers> getTbUsersAll() {
        tbUsersList=new ArrayList<TbUsers>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Users";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbUsersList.add(getTbUsers(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return tbUsersList;
    }
    public List<TbUsers> getTbUsersAll(VskPage vp) {
        tbUsersList=new ArrayList<TbUsers>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Users";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Users", "UserId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbUsersList.add(getTbUsers(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return tbUsersList;
    }
    public TbUsers getTbUsers(ResultSet rs) throws Exception{
        TbUsers tbUsers = new TbUsers();
        tbUsers.setUserId(rs.getInt("UserId"));
        tbUsers.setUserName(rs.getString("UserName"));
        tbUsers.setUserNickName(rs.getString("UserNickName"));
        tbUsers.setUserCardId(rs.getString("UserCardId"));
        tbUsers.setUserWorkId(rs.getString("UserWorkId"));
        tbUsers.setUserPass(rs.getString("UserPass"));
        tbUsers.setUserPhone(rs.getString("UserPhone"));
        tbUsers.setUserEmail(rs.getString("UserEmail"));
        tbUsers.setUserAddress(rs.getString("UserAddress"));
        tbUsers.setUserBirthday(rs.getString("UserBirthday"));
        tbUsers.setUserInCompanyTime(rs.getString("UserInCompanyTime"));
        tbUsers.setUserState(rs.getInt("UserState"));
        tbUsers.setParentUserId(rs.getInt("ParentUserId"));
        tbUsers.setCreateUserId(rs.getInt("CreateUserId"));
        tbUsers.setIsEnable(rs.getInt("IsEnable"));
        tbUsers.setUserMemo(rs.getString("UserMemo"));
        tbUsers.setCreateTime(rs.getString("CreateTime"));
        return tbUsers;
    }
    public static void main(String[] args) {
	}
}
